package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BillInfo;
import com.ruoyi.system.domain.BillInfo;
import com.ruoyi.system.mapper.BillInfoMapper;
import com.ruoyi.system.mapper.BillInfoMapper;
import com.ruoyi.system.service.IBillInfoService;
import com.ruoyi.system.service.IBillInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BillInfoServiceImpl implements IBillInfoService {

    @Autowired
    public BillInfoMapper billInfoMapper;

    @Override
    public List<BillInfo> selectBillInfoList(BillInfo billInfo) {
        return billInfoMapper.selectBillInfoList(billInfo);
    }

    @Override
    public int insertBillInfo(BillInfo billInfo) {
        billInfo.setBillId(UUID.randomUUID().toString().replaceAll("-", ""));
        concatBillTime(billInfo);
        return billInfoMapper.insertBillInfo(billInfo);
    }

    @Override
    public int updateBillInfo(BillInfo billInfo) {
        concatBillTime(billInfo);
        return billInfoMapper.updateBillInfo(billInfo);
    }

    @Override
    public BillInfo selectBillInfoById(String billId) {
        return billInfoMapper.selectBillInfoById(billId);
    }

    @Override
    public int deleteBillInfoByIds(String ids) {
        return billInfoMapper.deleteBillInfoByIds(Convert.toStrArray(ids));
    }

    @Override
    public String validateDatas(List<BillInfo> lists) {
        StringBuilder sb =new StringBuilder();
        if (StringUtils.isEmpty(lists)){
            sb.append("excel中无任何数据，请确认文件！");
        }else {
            for (int i = 0; i <lists.size() ; i++) {
                if (StringUtils.isBlank(lists.get(i).getCompanyName())){
                    sb.append("第"+(i+1)+"行、用户单位名称为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getProjectName())){
                    sb.append("第"+(i+1)+"行、用户项目名称为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getChargeType())){
                    sb.append("第"+(i+1)+"行、收费类型为空！<br/>");
                }
                if (Double.isNaN(lists.get(i).getBillCount())){
                    sb.append("第"+(i+1)+"行、开票金额为空！<br/>");
                }
                if (Double.isNaN(lists.get(i).getReceivedCount())){
                    lists.get(i).setReceivedCount(lists.get(i).getBillCount());
                }
                if (StringUtils.isBlank(lists.get(i).getBillNumber())){
                    sb.append("第"+(i+1)+"行、发票号为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getReceivedStatus())){
                    lists.get(i).setReceivedStatus("0");
                }
            }
        }
        if (sb.length()>0){
            return sb.toString();
        };
        return "";
    }

    @Override
    public String importRelations(List<BillInfo> lists, boolean updateSupport, SysUser sysUser) {
        int successNum = 0;
        int failureNum = 0;
        StringBuffer successMsg = new StringBuffer();
        StringBuffer failureMsg = new StringBuffer();
        for (BillInfo sb:lists){
            concatBillTime(sb);
            try {
                if (StringUtils.isNotEmpty(sb.getBillId())){
                    System.out.println(billInfoMapper.selectBillInfoById(sb.getBillId()));
                    if (billInfoMapper.selectBillInfoById(sb.getBillId()) == null) {
                        billInfoMapper.insertBillInfo(sb);
                    }else {
                        billInfoMapper.updateBillInfo(sb);
                    }
                }else {
                    sb.setBillId(UUID.randomUUID().toString().replaceAll("-", ""));
                    billInfoMapper.insertBillInfo(sb);
                }
                successNum++;
                successMsg.append("<br/>第" + (successNum+failureNum) + "条、" + sb.getBillId()+ "导入成功");
            }catch (Exception e){
                failureNum ++;
                failureMsg.append("<br/>第"+(successNum+failureNum)+"条、导入失败");
            }
        }
        if(failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共"+failureNum+"条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您数据已全部导入成功！共"+ successNum +"条，数据如下：");
        }
        return successMsg.toString();
    }











    /* 拼接开票时间开始+结束 */
    public void concatBillTime(BillInfo billInfo){
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy.MM.dd");
        Date start = billInfo.getBillingTimeStart();
        Date end = billInfo.getBillingTimeEnd();
        billInfo.setBillingTimeConcat(sbf.format(start)+"-"+sbf.format(end));
    }


}
